﻿using System;
using System.Security.Permissions;
using System.Windows.Forms;
using Devv.FreshTracker.Properties;

namespace Devv.FreshTracker
{
	public partial class MiniForm : Form
	{
		#region Properties

		private const Int32 WM_NCHITTEST = 0x84;
		private const Int32 WM_NCLBUTTONDOWN = 0xA1;
		private const Int32 HTCLIENT = 0x1;
		private const Int32 HTCAPTION = 0x2;

		private DateTime LastClick { get; set; }
		private Boolean Colon { get; set; }

		#endregion

		#region Main methods

		public MiniForm()
		{
			InitializeComponent();

			Colon = true;
		}

		private void MiniForm_Load(object sender, EventArgs e)
		{
			Left = Program.MainFormObj.Right - Width - 1;
			Top = Program.MainFormObj.Bottom - Height - 1;

			if (SessionTimer.Running)
			{
				Started();
			}
			else
			{
				Paused();
			}
		}

		private void MiniForm_DoubleClick(object sender, EventArgs e)
		{
			Program.MainFormObj.ShowToUser();

			CloseWindow();
		}

		private void CloseWindow()
		{
			Close();

			if (!IsDisposed)
			{
				Dispose();
			}
		}

		#endregion

		#region Moving the window



		private void MiniForm_MouseDown(object sender, MouseEventArgs e)
		{
			if (e.Button == MouseButtons.Left)
			{
				if (LastClick.AddMilliseconds(700) >= DateTime.Now)
				{
					MiniForm_DoubleClick(this, null);
					return;
				}

				try
				{
					NativeMethods.ReleaseCapture();
					NativeMethods.SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
				}
				catch
				{
					// Oops, could not invoke User32.dll
					// probably running outside Windows environment
				}

				LastClick = DateTime.Now;
			}
		}

		[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
		protected override void WndProc(ref Message m)
		{
			try
			{
				switch (m.Msg)
				{
					case WM_NCHITTEST:

						base.WndProc(ref m);

						Int32 r = (Int32)m.Result;

						if (r == HTCLIENT)
						{
							m.Result = (IntPtr)HTCAPTION;
						}

						return;
				}
			}
			catch
			{
				// Probably running outside Windows
			}

			base.WndProc(ref m);
		}

		#endregion

		#region Context menu

		private void menuHide_Click(object sender, EventArgs e)
		{
			Options.ShowMini = false;

			CloseWindow();
		}

		#endregion

		#region Time tracking

		private void imgTimer_Click(object sender, EventArgs e)
		{
			if (SessionTimer.Running)
			{
				Program.MainFormObj.TimerPause();
			}
			else
			{
				Program.MainFormObj.TimerStart();
			}
		}

		private void timerClock_Tick(object sender, EventArgs e)
		{
			String sep = Colon || !SessionTimer.Running ? ":" : " ";
			String hours = lblTimer.Text.Substring(0, 2);
			String minutes = lblTimer.Text.Substring(3, 2);

			lblTimer.Text = hours + sep + minutes;

			Colon = !Colon;
		}

		public void SetTimer(TimeSpan ts)
		{
			String sep = Colon ? ":" : " ";

			lblTimer.Text = ts.Hours.ToString("00") + sep + ts.Minutes.ToString("00");
		}

		public void Started()
		{
			toolTips.SetToolTip(lblTimer, Messages.TimerActive);
			imgTimer.Image = Resources.IcoMiniPause;
		}

		public void Paused()
		{
			toolTips.SetToolTip(lblTimer, Messages.Idle);
			imgTimer.Image = Resources.IcoMiniPlay;
		}

		#endregion
	}
}